package com.company.cloud.admin.modules.admin.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.company.cloud.admin.modules.cache.models.PermissionModel;
import com.company.cloud.admin.modules.cache.utils.PTUserConvertUtils;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.tree.CloudTree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.cloud.admin.constant.PlatformAdminConstant;
import com.company.cloud.admin.constant.PlatformAdminErrorEnum;
import com.company.cloud.admin.modules.admin.mapper.PermissionQueryMapper;
import com.company.cloud.admin.modules.admin.models.MenuResultInfo;
import com.company.cloud.admin.modules.admin.service.PermissionMgrService;
import com.company.cloud.admin.modules.base.entity.PermissionInfo;
import com.company.cloud.admin.modules.base.entity.RolePermissionInfo;
import com.company.cloud.admin.modules.base.service.PermissionInfoService;
import com.company.cloud.admin.modules.base.service.RolePermissionInfoService;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.exception.BusinessException;

@Service
public class PermissionMgrServiceImpl implements PermissionMgrService {

	@Autowired
	private PermissionInfoService permissionInfoService;
	@Autowired
	private RolePermissionInfoService rolePermissionInfoService;
	@Autowired
	private PermissionQueryMapper permissionQueryMapper;
	
	
	@Override
	public List<PermissionModel> getMenuTree(Integer userId, String showApp){
		Set<String> showAppSet=new HashSet<String>();
		showAppSet.add(PlatformAdminConstant.PERMISSION_SHOW_ALL);
		if(!StringUtils.isEmpty(showApp)) {
			showAppSet.add(showApp);
		}
		List<PermissionInfo> permissionList=permissionQueryMapper.queryUserMenu(new QueryWrapper<PermissionInfo>()
				.eq("a.user_id", userId)
				.in("c.type", PlatformAdminConstant.PermissionTypeEnum.P_Menu.getCode())
				.orderByDesc("c.parent_code","c.orders")
		);
		return this.getTree(permissionList);
	}
	
	@Override
	public MenuResultInfo getMenu2ButtonTree(Integer userId, String showApp) {
		Set<String> showAppSet=new HashSet<String>();
		showAppSet.add(PlatformAdminConstant.PERMISSION_SHOW_ALL);
		if(!StringUtils.isEmpty(showApp)) {
			showAppSet.add(showApp);
		}
		List<PermissionInfo> permissionList=permissionQueryMapper.queryUserMenu(new QueryWrapper<PermissionInfo>()
				.eq("a.user_id", userId)
				.in("c.type", PlatformAdminConstant.PermissionTypeEnum.P_Menu.getCode(), PlatformAdminConstant.PermissionTypeEnum.P_Button.getCode())
				.orderByDesc("c.parent_code","c.orders")
		);
		
		MenuResultInfo result=new MenuResultInfo();
		if(CollectionUtils.isEmpty(permissionList)) {
			return result;
		}

		List<PermissionInfo> menuList=new ArrayList<PermissionInfo>();
		for(PermissionInfo permission:permissionList) {
			if(PlatformAdminConstant.PermissionTypeEnum.P_Menu.getCode().equals(permission.getType())) {
				menuList.add(permission);
			}else if(PlatformAdminConstant.PermissionTypeEnum.P_Button.getCode().equals(permission.getType())) {
				result.addButton(PTUserConvertUtils.getPermissionModel(permission));
			}
		}
		result.setMenus(this.getTree(menuList));
		return result;
	}

	@Override
	public List<PermissionModel> getPermissionTree() {
		List<PermissionInfo> permissionList=permissionInfoService.list();
		return this.getTree(permissionList);
	}
	
	@Override
	public void delete(String code, LoginSimpleUserInfo userInfo) {
		PermissionInfo entity=permissionInfoService.getById(code);
		if(entity==null) {
			throw new BusinessException(PlatformAdminErrorEnum.PERMISSION_NULL);
		}
		int childrenNum= permissionInfoService.count(new QueryWrapper<PermissionInfo>().eq("pcode", code));
		if( childrenNum > 0 ) {
			throw new BusinessException(PlatformAdminErrorEnum.PERMISSION_HAS_CHILDREN);
		}
		permissionInfoService.removeById(code);
		
		rolePermissionInfoService.remove(new QueryWrapper<RolePermissionInfo>().eq("per_code", code));
		//重新加载角色权限关系缓存
	}
	
	
	private List<PermissionModel> getTree(List<PermissionInfo> items){
		if(CollectionUtils.isEmpty(items)){
			return new ArrayList<>();
		}
		CloudTree<PermissionInfo, PermissionModel> tree=new CloudTree<PermissionInfo, PermissionModel>() {
			@Override
			public boolean isTop(PermissionModel entity) {
				return PlatformAdminConstant.PERMISSION_TOP_CODE.equals(entity.getParentCode());
			}
			@Override
			public boolean isParent(PermissionModel parentObj, PermissionModel childObj) {
				return parentObj.getCode().equals(childObj.getParentCode());
			}
			@Override
			public PermissionModel convert(PermissionInfo entity) {
				return PTUserConvertUtils.getPermissionModel(entity);
			}
		};

		return tree.tree(items);
	}

	
	
}
